package com.xy.server.util;

import java.io.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

public class ThesisUtil {

    public static void main(String[] args) throws Exception{

        String folderRead = "/Users/agan/Desktop/论文整理二-摘要";
        String folderWrite = "整理";

        delAllFile(folderRead + "/" + folderWrite);
        mkDirectory(folderRead + "/" + folderWrite);

        List<String> filePathList = scanFiles(folderRead);

        for(String readFile:filePathList) {
            String fileName = readFile.substring(readFile.lastIndexOf("/") + 1);
            if(fileName.equals(folderWrite)){
                continue;
            }
            readLine(readFile, folderRead + "/" + folderWrite + "/" + fileName);
        }
        return ;

    }

    public static void readLine(String readFile, String writerFile) {
        try {


            BufferedWriter bw = new BufferedWriter(new FileWriter(writerFile));

            FileReader fr = new FileReader(readFile);
            BufferedReader br = new BufferedReader(fr);

            String str;
            String paragraph = "";
            int emptyLineCount = 0;
            while ((str = br.readLine()) != null) {
                System.out.println(str);
                if(!str.trim().equals("")){
                    paragraph += str;
                    emptyLineCount = 0;
                }else{
                    emptyLineCount++;
                    if(emptyLineCount == 1) {
                        bw.write(paragraph);
                        bw.newLine();
                        bw.newLine();
                        paragraph = "";
                    }
                }
            }
            bw.write(paragraph);

            br.close();
            bw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



    public static List<String> scanFiles(String path) {
        List<String>filePaths = new ArrayList<String>();
        LinkedList<File> list = new LinkedList<File>();
        File dir = new File(path);
        File[] file = dir.listFiles();

        for (int i = 0; i < file.length; i++) {
            if (file[i].isDirectory()) {
                // 把第一层的目录，全部放入链表
                list.add(file[i]);
            }
            filePaths.add(file[i].getAbsolutePath());
        }
        // 循环遍历链表
        while (!list.isEmpty()) {
            // 把链表的第一个记录删除
            File tmp = list.removeFirst();
            // 如果删除的目录是一个路径的话
            if (tmp.isDirectory()) {
                // 列出这个目录下的文件到数组中
                file = tmp.listFiles();
                if (file == null) {// 空目录
                    continue;
                }
                // 遍历文件数组
                for (int i = 0; i < file.length; ++i) {
                    if (file[i].isDirectory()) {
                        // 如果遍历到的是目录，则将继续被加入链表
                        list.add(file[i]);
                    }
                    filePaths.add(file[i].getAbsolutePath());
                }
            }
        }
        return filePaths;
    }

    public static boolean mkDirectory(String path) {
        File file = null;
        try {
            file = new File(path);
            if (!file.exists()) {
                return file.mkdirs();
            }
            else{
                return false;
            }
        } catch (Exception e) {
        } finally {
            file = null;
        }
        return false;
    }

    public static void delFolder(String folderPath) {
        try {
            delAllFile(folderPath); //删除完里面所有内容
            String filePath = folderPath;
            filePath = filePath.toString();
            java.io.File myFilePath = new java.io.File(filePath);
            myFilePath.delete(); //删除空文件夹
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static boolean delAllFile(String path) {
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        String[] tempList = file.list();
        File temp = null;
        for (int i = 0; i < tempList.length; i++) {
            if (path.endsWith(File.separator)) {
                temp = new File(path + tempList[i]);
            } else {
                temp = new File(path + File.separator + tempList[i]);
            }
            if (temp.isFile()) {
                temp.delete();
            }
            if (temp.isDirectory()) {
                delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
                delFolder(path + "/" + tempList[i]);//再删除空文件夹
                flag = true;
            }
        }
        return flag;
    }
}

